package com.clkj.cldisabledmanclient.mvp;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.clkj.cldisabledmanclient.R;
import com.clkj.cldisabledmanclient.base.BaseActivity;
import com.clkj.cldisabledmanclient.http.HttpConfig;
import com.github.chrisbanes.photoview.PhotoView;
import com.github.chrisbanes.photoview.PhotoViewAttacher;

import java.util.ArrayList;
import java.util.List;



public class ViewPhotoActivity extends BaseActivity {
    private static final String TAG_PHOTO = "photos";
    private static final String TAG_INDEX = "index";
    private ArrayList<String> picUrls = new ArrayList<>();
    private int position;

    private TextView tvIndicator;
    private ImageView ivBack;

    private boolean isLocalImage;

    public static Intent newIntent(Context context, ArrayList<String> pics, int index, boolean isLocalImg) {
        Intent intent = new Intent(context, ViewPhotoActivity.class);
        intent.putStringArrayListExtra(TAG_PHOTO, pics);
        intent.putExtra(TAG_INDEX, index);
        intent.putExtra("local", isLocalImg);
        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置无标题
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //设置全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_view_photo);
        ViewPager mViewPager = findViewById(R.id.view_pager);
        tvIndicator = (TextView) findViewById(R.id.tv_pic_indicator);
        ivBack = (ImageView) findViewById(R.id.iv_back);

        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ViewPhotoActivity.this.finish();
            }
        });

        picUrls = getIntent().getStringArrayListExtra(TAG_PHOTO);
        position = getIntent().getIntExtra(TAG_INDEX, 0);
        isLocalImage = getIntent().getBooleanExtra("local", false);

        mViewPager.setAdapter(new SamplePagerAdapter(picUrls));
        mViewPager.setCurrentItem(position);
        tvIndicator.setText((position + 1) + "/" + picUrls.size());
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                tvIndicator.setText((position + 1) + "/" + picUrls.size());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void initData() {

    }

    @Override
    public void assignView() {

    }

    @Override
    public void initView() {

    }

    class SamplePagerAdapter extends PagerAdapter {

        private List<String> urls;

        public SamplePagerAdapter(List<String> urls) {
            this.urls = urls;
        }

        @Override
        public int getCount() {
            return urls.size();
        }

        @Override
        public View instantiateItem(ViewGroup container, int position) {
            final PhotoView photoView = new PhotoView(container.getContext());
            final PhotoViewAttacher attacher = new PhotoViewAttacher(photoView);

            String url = HttpConfig.URL_OF_INTERFACE_BASE + urls.get(position);
            if (isLocalImage)
                url = urls.get(position);
            /*Picasso.with(ViewPhotoActivity.this)
                    .load(url)
                    .config(Bitmap.Config.RGB_565)
                    .placeholder(R.mipmap.ico_data_empty)
                    .into(photoView, new Callback() {
                        @Override
                        public void onSuccess() {
                            attacher.update();
                        }

                        @Override
                        public void onError() {
                        }
                    });*/

            RequestOptions requestOptions = new RequestOptions().fitCenter().placeholder(R.mipmap.ico_data_empty)
                    .error(R.mipmap.ico_data_empty);
            Glide.with(ViewPhotoActivity.this).load(url).apply(requestOptions)
                    .into(new SimpleTarget<Drawable>() {
                        @Override
                        public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                            photoView.setImageDrawable(resource);
                            attacher.update();
                        }
                    });

           /* attacher.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(final View view) {

                    new AlertDialog.Builder(ViewPhotoActivity.this)
                            .setMessage("保存图片")
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    PhotoUtil.saveBitmapFromImageView((ImageView)photoView);
                                }
                            })
                            .show();
                    return false;
                }
            });*/

            // Now just add PhotoView to ViewPager and return it
            container.addView(photoView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

            return photoView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

    }
}
